<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm" label="" class="w-[230px]">
          <a-input v-model:value="formState.zjm" placeholder="商品名称/助记码" />
        </a-form-item>
        <a-form-item name="dateRange" label="配镜时间" class="w-[320px] ml-3">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="categroyId" label="商品分类" class="w-[300px] ml-3">
          <a-select
            v-model:value="formState.categroyId"
            :options="categroyList"
            allow-clear
            placeholder="请选择"
            :field-names="{ label: 'categroyName', value: 'id' }"
          />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-2">
        <BaseExport
          type="视光"
          name="品名统计"
          code="000268"
          :search-params="{
            ...formState
          }"
          >导出</BaseExport
        >
      </div>
      <BaseTable fill bordered column-code="000268" :extra-height="50" :loading="loading" :data-source="tableData" :pagination="pagination">
        <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex == 'xh'">
            {{ index + 1 }}
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <template v-for="column in columnsData" :key="column.dataIndex">
                <a-table-summary-cell v-if="column.dataIndex === 'xh'"> 合计 </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'num'">
                  <div class="text-right">{{ summaryData.num || 0 }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'totalAmount'">
                  <div class="text-right">￥{{ formatAmount(summaryData.totalAmount || 0) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'amountCost'">
                  <div class="text-right">￥{{ formatAmount(summaryData.amountCost || 0) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else>-</a-table-summary-cell>
              </template>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { formatAmount } from '@/utils/format.js'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'

const formRef = ref(null)
const categroyList = ref([])
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/optometry/ooadolescentcheck/getOoOpticaionByPayltemldStatistic',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1]
    }
  },
  onSuccess: () => {
    getSummaryData()
  }
})
//获取表头
const columnsData = ref([])
const getTableColumns = async () => {
  const { code, data } = await post('/common/util/getTableMappingCloumnList', { id: '000268' })
  if (code !== 0) return message.error(code)
  columnsData.value = data.filter((item) => !item.isHide)
}
const summaryData = ref({})
const getSummaryData = async () => {
  const { code, data } = await post('/optometry/ooadolescentcheck/getOoOpticaionByPayltemldStatisticHJ', {
    ...formState.value,
    stdate: formState.value.dateRange?.[0],
    eddate: formState.value.dateRange?.[1]
  })
  if (code !== 0) return message.error(code)
  summaryData.value = data || {}
}

const getCategroyList = async () => {
  const { code, data } = await post('/operation/opex/getOoCategroyList', { status: 1 })
  if (code === 0) {
    categroyList.value = dataManipulation(data)
  }
}

//递归获取所有子节点children获取id
const dataManipulation = (data) => {
  return data.map((item) => {
    const childObjects = item.children && item.children.length > 0 ? dataManipulation(item.children) : []
    return {
      ...item,
      children: childObjects
    }
  })
}

onMounted(() => {
  getCategroyList()
  getTableColumns()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
